package com.dgut.forum.controller;

import com.dgut.forum.common.AppResult;
import com.dgut.forum.common.ResultCode;
import com.dgut.forum.exception.ApplicationException;
import com.dgut.forum.model.Board;
import com.dgut.forum.services.impl.BoardServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Api(tags="版块接口")
@Slf4j
@RequestMapping("/board")
@RestController
public class BoardController {

    @Value("${forum.index.board-num:9}")//拿到yml的值,默认是9
    private Integer boardNum;
    @Autowired
    private BoardServiceImpl boardService;

    @ApiOperation("获取版块信息")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        log.info("获取首页版块个数"+boardNum);
        List<Board> boards=boardService.selectByNum(boardNum);
        if(boards==null){
            log.info("获取不到板块信息");
            boards=new ArrayList<>();
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS ));
        }
        return AppResult.success(boards);
    }

    @ApiOperation("获取版块信息")
    @GetMapping("/getBoardInfoById")
    public AppResult getBoardInfoById(@ApiParam("版块id") @RequestParam("id") @NonNull Long id){
        if(id<0){
            log.info("获取版块信息 id不合法 id="+id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board=boardService.selectById(id);
        if(board==null){
            log.info("获取不到板块信息");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS ));
        }
        return AppResult.success(board);
    }

}
